package com.topsec.manager.util;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：拆分工具的url
 *
 * @Idea IntelliJ IDEA
 * @Since JDK：1.8.0_102
 * @Date 2018-12-11 10:04
 * @Program app-web
 * @ClassName SplitToolUrlUtil
 * @Version V1.0
 * @Author hyq
 */
public class SplitToolUrlUtil {


    /**
     * 说明：判断当前工具的url是哪个市场的
     *
     * @param urlStr 工具的url
     * @description TODO：
     * @author hyq
     * @date 2018/12/11 10:11
     */
    public static String getUrlMarket(String urlStr) {
        String reStr = "";
        try {
            String[] oneUrl = SplitUrl.splitUrls(urlStr);

            if (oneUrl.length >= 3) {
                String url_Www = oneUrl[2];

                if (url_Www.contains("play.google")) {
                    reStr = "play.google";
                } else if (url_Www.contains("itunes.apple")) {
                    reStr = "itunes.apple";
                } else if (url_Www.contains("chrome.google")) {
                    reStr = "chrome.google";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reStr;
    }

    /**
     * 说明：将需要验证的工具的url的id拆分出来
     *
     * @param
     * @return
     * @description TODO：
     * @author hyq
     * @date 2018/12/11 10:21
     */
    public static String splitToolUrl(String tUrl, String tag) {
        String reUrlId = "";
        try {
            String[] oneUrl = SplitUrl.splitUrls(tUrl);

            if (oneUrl.length >= 3) {
                String url_Www = oneUrl[2];
                String url_Context = oneUrl[3];

                if ("play.google".equals(tag) && url_Www.contains("play.google")) {
                    String[] ids = url_Context.split("\\?id=");
                    if (ids.length >= 2) {
                        String id = ids[1];

                        reUrlId = id;
                    }
                } else if ("itunes.apple".equals(tag) && url_Www.contains("itunes.apple")) {
                    String[] ids = url_Context.split("id");
                    if (ids.length >= 2) {
                        String id = ids[1];

                        if (id.contains("?mt=8")) {
                            String substring = id.substring(0, id.indexOf("?mt"));
                            reUrlId = substring;
                        } else {
                            reUrlId = id;
                        }
                    }
                } else if ("chrome.google".equals(tag) && url_Www.contains("chrome.google")) {
                    if (url_Context.contains("detail/")) {
                        String[] split = url_Context.split("detail/");
                        if (split.length >= 2) {
                            String google = split[1];

                            if (google.contains("/")) {
                                if (google.contains("?")) {
                                    String substring = google.substring(google.lastIndexOf("/") + 1, google.indexOf("?"));
                                    reUrlId = substring;
                                } else {
                                    String substring = google.substring(google.lastIndexOf("/") + 1, google.length());
                                    reUrlId = substring;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reUrlId;
    }

    /**
     * 说明：拆分工具表中工具的url，并返回对应市场的url的list
     *
     * @param urlList url的list集合
     * @param tag     要组装的市场list的标记
     * @description TODO：
     * @author hyq
     * @date 2018/12/11 10:05
     */
    public static List<String> getAllUrlIdList(List<String> urlList, String tag) {

        List<String> reList = new ArrayList<>();

        try {
            for (String allUrls : urlList) {

                if (StringUtils.isNotEmpty(allUrls) && null != allUrls) {
                    boolean contains = allUrls.contains(";");
                    // 多条url
                    if (contains) {
                        String[] split = allUrls.split(";");
                        for (String oneUrl : split) {

                            String[] urlStr = SplitUrl.splitUrls(oneUrl);

                            if (urlStr.length >= 3) {
                                String url_Www = urlStr[2];
                                String url_Context = urlStr[3];

                                if ("play.google".equals(tag) && url_Www.contains("play.google")) {
                                    String[] ids = url_Context.split("\\?id=");
                                    if (ids.length >= 2) {
                                        String id = ids[1];

                                        reList.add(id);
                                    }
                                } else if ("itunes.apple".equals(tag) && url_Www.contains("itunes.apple")) {
                                    String[] ids = url_Context.split("id");
                                    if (ids.length >= 2) {
                                        String id = ids[1];

                                        if (id.contains("?mt=8")) {
                                            String substring = id.substring(0, id.indexOf("?mt"));
                                            reList.add(substring);
                                        } else {
                                            reList.add(id);
                                        }
                                    }
                                } else if ("chrome.google".equals(tag) && url_Www.contains("chrome.google")) {
                                    if (url_Context.contains("detail/")) {
                                        String[] splitt = url_Context.split("detail/");
                                        if (splitt.length >= 2) {
                                            String google = splitt[1];

                                            if (google.contains("/")) {
                                                if (google.contains("?")) {
                                                    String substring = google.substring(google.lastIndexOf("/") + 1, google.indexOf("?"));
                                                    reList.add(substring);
                                                } else {
                                                    String substring = google.substring(google.lastIndexOf("/") + 1, google.length());
                                                    reList.add(substring);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        String[] oneUrl = SplitUrl.splitUrls(allUrls);

                        if (oneUrl.length >= 3) {
                            String url_Www = oneUrl[2];
                            String url_Context = oneUrl[3];

                            if ("play.google".equals(tag) && url_Www.contains("play.google")) {
                                String[] ids = url_Context.split("\\?id=");
                                if (ids.length >= 2) {
                                    String id = ids[1];

                                    reList.add(id);
                                }
                            } else if ("itunes.apple".equals(tag) && url_Www.contains("itunes.apple")) {
                                String[] ids = url_Context.split("id");
                                if (ids.length >= 2) {
                                    String id = ids[1];

                                    if (id.contains("?mt=8")) {
                                        String substring = id.substring(0, id.indexOf("?mt"));
                                        reList.add(substring);
                                    } else {
                                        reList.add(id);
                                    }
                                }
                            } else if ("chrome.google".equals(tag) && url_Www.contains("chrome.google")) {
                                if (url_Context.contains("detail/")) {
                                    String[] split = url_Context.split("detail/");
                                    if (split.length >= 2) {
                                        String google = split[1];

                                        if (google.contains("/")) {
                                            if (google.contains("?")) {
                                                String substring = google.substring(google.lastIndexOf("/") + 1, google.indexOf("?"));
                                                reList.add(substring);
                                            } else {
                                                String substring = google.substring(google.lastIndexOf("/") + 1, google.length());
                                                reList.add(substring);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return reList;
    }
}
